/*
 * 
 */
package dao;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import data.SpriteDefinition;
import data.Vector2;
import entity.Troop;
import entity.map.Map;

/**
 * 
 * @author YvensDesktop
 *
 */
public class ReaderXml {

	private final String CONFIG_XML = "content/xml/config.xml";
	private final String MAP_CONFIG_XML = "content/xml/map-config.xml";
	private final String SPRITE_CONFIG_XML = "content/xml/sprite-config.xml";
	private final String TROOP_CONFIG_XML = "content/xml/troop-config.xml";

	ReaderXml() {}
	
	public LinkedHashMap<String, Troop> readTroop(){
		try{
			LinkedHashMap<String, Troop> hashTroop = new LinkedHashMap<String, Troop>();
			
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(TROOP_CONFIG_XML));
            
            doc.getDocumentElement().normalize ();
            
            NodeList listOfTroops = doc.getElementsByTagName("troop");
            
            for (int i = 0; i < listOfTroops.getLength(); i++) {
				Node troopNode = listOfTroops.item(i);
				if(troopNode.getNodeType() == Node.ELEMENT_NODE){
					Element troopElement = (Element)troopNode;
					
					String name = troopElement.getAttribute("name");
					int strenght = Integer.parseInt(troopElement.getAttribute("strenght"));
					int defense = Integer.parseInt(troopElement.getAttribute("defense"));
					int health = Integer.parseInt(troopElement.getAttribute("health"));
					int movement = Integer.parseInt(troopElement.getAttribute("movement"));
					float hitRatio = Float.parseFloat(troopElement.getAttribute("hitRatio"));
					String spritePath = troopElement.getAttribute("spritePath");
					
					Troop troop = new Troop(false, name, strenght, defense, health, movement, hitRatio, spritePath);
					
					hashTroop.put(name, troop);
				}
			}
            return hashTroop;
			
		} catch (ParserConfigurationException e) {
			System.out.println("Erro ao ler o XML de Tropas");
		} catch (SAXException e) {
			System.out.println("Erro ao ler o XML de Tropas");
		} catch (IOException e) {
			System.out.println("Erro ao ler o XML de Tropas");
		} 
		
		return null;
	}
	
	public LinkedHashMap<String, Map> readMap(){
		try{
			LinkedHashMap<String, Map> hashMap = new LinkedHashMap<String, Map>();
			
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(MAP_CONFIG_XML));
            
            doc.getDocumentElement().normalize ();
            
            NodeList listOfTroops = doc.getElementsByTagName("map");
            
            for (int i = 0; i < listOfTroops.getLength(); i++) {
				Node mapNode = listOfTroops.item(i);
				if(mapNode.getNodeType() == Node.ELEMENT_NODE){
					Element mapElement = (Element)mapNode;
					
					String name = mapElement.getAttribute("name");
					String file = mapElement.getAttribute("file");
					int width = Integer.parseInt(mapElement.getAttribute("width"));
					int height = Integer.parseInt(mapElement.getAttribute("height"));
					int row = Integer.parseInt(mapElement.getAttribute("rows"));
					int column = Integer.parseInt(mapElement.getAttribute("columns"));
					
					
					Map map = new Map(name, file, width, height, row, column);
					
					hashMap.put(name, map);
				}
			}
            return hashMap;
			
		} catch (ParserConfigurationException e) {
			System.out.println("Erro ao ler o XML de mapas");
		} catch (SAXException e) {
			System.out.println("Erro ao ler o XML de mapas");
		} catch (IOException e) {
			System.out.println("Erro ao ler o XML de mapas");
		} 
		
		return null;
	}
	
	public LinkedHashMap<String, SpriteDefinition> readSpriteChar(){
		try{
			LinkedHashMap<String, SpriteDefinition> hashSpriteChar = new LinkedHashMap<String, SpriteDefinition>();
			
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(SPRITE_CONFIG_XML));
            
            doc.getDocumentElement().normalize ();
            
            NodeList listOfSprites = doc.getElementsByTagName("sprite");

            for (int i = 0; i < listOfSprites.getLength(); i++) {
				Node spriteCharNode = listOfSprites.item(i);
				if(spriteCharNode.getNodeType() == Node.ELEMENT_NODE){
					Element spriteCharElement = (Element)spriteCharNode;
					
					String name = spriteCharElement.getAttribute("name");
					String file = spriteCharElement.getAttribute("file");
					int columnCount = Integer.parseInt(spriteCharElement.getAttribute("column"));
					int lineCount = Integer.parseInt(spriteCharElement.getAttribute("line"));
					
					SpriteDefinition spriteChar = new SpriteDefinition(name, file, columnCount, lineCount);
					hashSpriteChar.put(spriteChar.getName(), spriteChar);
				}
			}
            return hashSpriteChar;
		} catch (ParserConfigurationException e) {
			System.out.println("Erro ao ler o XML de Sprites");
		} catch (SAXException e) {
			System.out.println("Erro ao ler o XML de Sprites");
		} catch (IOException e) {
			System.out.println("Erro ao ler o XML de Sprites");
		} 
		
		return null; // se der algum erro retorna isso.
	}
	
	public LinkedHashMap<String, Object> readConfig(){
		try{
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(CONFIG_XML));
            
            doc.getDocumentElement ().normalize ();
            LinkedHashMap<String, Object> hashConfig = new LinkedHashMap<String, Object>();
            
            NodeList nodeList = doc.getElementsByTagName("resolution");
            Element element = (Element) nodeList.item(0);
            int x = Integer.parseInt(element.getAttribute("x"));
            int y = Integer.parseInt(element.getAttribute("y"));
            Vector2 resolutionVector = new Vector2(x, y);
            hashConfig.put("resolution", resolutionVector);
            
            nodeList = doc.getElementsByTagName("arrowpath");
            element = (Element) nodeList.item(0);
            String arrowPath = element.getAttribute("path");
            hashConfig.put("arrowPath", arrowPath);
        
            nodeList = doc.getElementsByTagName("arrowsize");
            element = (Element) nodeList.item(0);
            x = Integer.parseInt(element.getAttribute("x"));
            y = Integer.parseInt(element.getAttribute("y"));
            Vector2 arrowSizeVector = new Vector2(x, y);
            hashConfig.put("arrowSize", arrowSizeVector);
            
            nodeList = doc.getElementsByTagName("titlescreenpath");
            element = (Element) nodeList.item(0);
            String titleScreenPath = element.getAttribute("path");
            hashConfig.put("titleScreenPath", titleScreenPath);
            
            nodeList = doc.getElementsByTagName("windowpath");
            element = (Element) nodeList.item(0);
            String windowPath = element.getAttribute("path");
            hashConfig.put("windowPath", windowPath);
            
            return hashConfig;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		return null;
	}
}
